import math
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Embedding, Dropout, Dense, Reshape,Flatten,merge
from keras.layers.merge import Dot, Concatenate,Add
from keras.models import Model, Input
from keras.optimizers import Adam

ratings = pd.read_csv('/data/quick_keras/data/ratings.dat', sep = '::', \
    engine='python',names = ['user_id','movie_id','rating','timestamp'])
n_users = np.max(ratings['user_id'])
n_movies = np.max(ratings['movie_id'])
print([n_users, n_movies, len(ratings)])

plt.hist(ratings['rating'])
plt.show()
print(np.mean(ratings['rating']))

n_factors = 128
user_in = Input(shape=(1,), dtype='int64', name='user_in')
u = Embedding(n_users, n_factors, input_length=1)(user_in)
movie_in = Input(shape=(1,), dtype='int64', name='movie_in')
v = Embedding(n_movies, n_factors, input_length=1)(movie_in)
x = Add([u, v], mode='dot')
x = Flatten()(x)
model = Model([user_in, movie_in], x)
model.compile(Adam(0.001), loss='mse')
model.fit([ratings['user_id'], ratings['movie_id']], ratings['rating'], batch_size=64, epochs=1)


